package com.atguigu.tingshu.order.helper;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.order.service.OrderInfoService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.redisson.api.RBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.security.sasl.RealmCallback;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * projectName: tingshu-parent
 *
 * @author: 王宝贵
 * time: 2024/11/8 18:43 周五
 * description:
 */
@Slf4j
@Component
public class DelayMsgConsumer {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderInfoService orderInfoService;


    @PostConstruct
    public void orderCancal(){
        log.info("开始监听延迟消息1");
        //1.创建阻塞队列
        RBlockingQueue<String> blockingQueue = redissonClient.getBlockingQueue(RedisConstant.CACHE_INFO_PREFIX);
        //开启线程监听阻塞队列中的消息，只需要单一核心线程池对象即可
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        log.info("开始监听延迟消息2");
        executorService.submit(()->{
            while (true){
                String take =null;
                try {
                    log.info("接收消息ing.............");
                    //监听到的消息
                    take = blockingQueue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                if(take !=null){
                    log.info("监听到延迟关单消息：======================================================{}",take);
                    //查询订单信息 取消订单
                   orderInfoService.orderCanncal(Long.valueOf(take));
                }

            }
        });
    }

}